﻿package com.ui.controls {
	import com.ui.controls.TextArea.RichTextField;
	import com.ui.core.UIComponent;
	import com.ui.data.ScrollBarData;
	import com.ui.data.TextAreaData;
	import com.ui.events.GScrollBarEvent;
	import com.ui.manager.UIManager;

	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	/**
	 * Game Text Area
	 * 
	 * @author Cafe
	 * @version 20100727
	 */
	public class TextArea extends UIComponent {
		protected var _data : TextAreaData;
		//protected var _backgroundSkin : DisplayObject;
		protected var _textField : TextField;
		protected var _temptextField : TextField;
		protected var _vScrollBar : ScrollBar;
		protected var _hScrollBar : ScrollBar;
		protected var _textWidth : Number;
		protected var _textHeight : Number;
		protected var _lock : Boolean = false;
		protected var _nowRow : int = 0;
		protected var _output : RichTextField;
		protected var _contentArr : Array;

		override protected function create() : void {
			//_backgroundSkin = UIManager.getUI(_data.backgroundAsset);
			_output = new RichTextField();
			_output.defaultTextFormat = _data.textFormat;
			_output.setSize(_data.width, _data.height);

			_textField = _output.textfield;
			_textField.x = _textField.y = _data.padding;
			_textField.multiline = true;
			_textField.wordWrap = true;
			_textField.condenseWhite = true;
			_textField.background = false;
			_textField.maxChars = _data.maxChars;
			_textField.defaultTextFormat = _data.textFormat;
			_textField.textColor = _data.textColor;
			_textField.filters = _data.textFieldFilters;

			_temptextField = new TextField();
			_temptextField.defaultTextFormat = _data.textFormat;
			_temptextField.textColor = _data.textColor;
			_temptextField.filters = _data.textFieldFilters;

			if (_data.editable) {
				_textField.styleSheet = null;
				_temptextField.styleSheet = null;
				_textField.type = TextFieldType.INPUT;
			} else {
				_textField.type = TextFieldType.DYNAMIC;
				_textField.styleSheet = _data.styleSheet;
				_temptextField.styleSheet = _data.styleSheet;
				_output.css = _data.styleSheet;
			}
			_textField.selectable = _data.selectable;
			// var data : ScrollBarData = new ScrollBarData();
			// data.visible = false;
			_vScrollBar = new ScrollBar(_data.vScrollBarData);
			// data = new ScrollBarData();
			// data.
			// data.visible = false;
			_hScrollBar = new ScrollBar(_data.hScrollBarData);

			_contentArr = new Array();

			//addChild(_backgroundSkin);
			addChild(_output);
			addChild(_vScrollBar);
			addChild(_hScrollBar);
		}

		override public function set width(value : Number) : void {
			super.width = value;
			_output.setSize(value, super.height);
		}

		override public function set height(value : Number) : void {
			super.height = value;
			_output.setSize(super.width, value);
		}

		override protected function layout() : void {
			//_backgroundSkin.width = _width;
			//_backgroundSkin.height = _height;
			_textField.width = _width - _data.padding * 2;
			_textField.height = _height - _data.padding * 2;
			reset();
		}

		override protected function onShow() : void {
			reset();
			_textField.addEventListener(Event.SCROLL, textFieldScrollHandler);
		}

		override protected function onHide() : void {
			_textField.removeEventListener(Event.SCROLL, textFieldScrollHandler);
		}

		protected function reset() : void {
			var needHScroll : Boolean = _textField.maxScrollH > 0;
			var needVScroll : Boolean = vScrollMax > 0;
			var newWidth : int = _width - (needVScroll ? _vScrollBar.width : 0);
			var newHeight : int = _height - (needHScroll ? _hScrollBar.height : 0);
			_data.vscrollalignToLeft && needVScroll ? _output.x = _base.x + _vScrollBar.width : _output.x = 0;
			_textField.width = newWidth - _data.padding * 2;
			_textWidth = _textField.textWidth;
			_textField.height = newHeight - _data.padding * 2;
			_textHeight = _textField.textHeight;
			if (needVScroll) {
				_data.vscrollalignToLeft ? _vScrollBar.x = 0 : _vScrollBar.x = _width - _vScrollBar.width;
				_vScrollBar.height = newHeight;
				_vScrollBar.resetValue(_textField.bottomScrollV - _textField.scrollV + 1, 0, vScrollMax, _textField.scrollV - 1);
				if (!_vScrollBar.visible) {
					_vScrollBar.visible = true;
					_vScrollBar.addEventListener(GScrollBarEvent.SCROLL, scrollHandler, false, 0, true);
				}
			} else if (_vScrollBar.visible) {
				_vScrollBar.removeEventListener(GScrollBarEvent.SCROLL, scrollHandler, false);
				_vScrollBar.visible = false;
			}
			if (needHScroll) {
				_hScrollBar.y = _height - _hScrollBar.height;
				_hScrollBar.width = newWidth;
				_hScrollBar.resetValue(_textField.width, 0, _textField.maxScrollH, Math.min(_textField.maxScrollH, _textField.scrollH));
				if (!_hScrollBar.visible) {
					_hScrollBar.visible = true;
					_hScrollBar.addEventListener(GScrollBarEvent.SCROLL, scrollHandler, false, 0, true);
				}
			} else if (_hScrollBar.visible) {
				_hScrollBar.removeEventListener(GScrollBarEvent.SCROLL, scrollHandler, false);
				_hScrollBar.visible = false;
			}
		}

		override protected function onEnabled() : void {
			_textField.type = (_enabled ? (_data.editable ? TextFieldType.INPUT : TextFieldType.DYNAMIC) : TextFieldType.DYNAMIC);
		}

		protected function get vScrollMax() : int {
			var max : int = _textField.numLines - _textField.bottomScrollV + _textField.scrollV - 1;
			return Math.min(max, _textField.maxScrollV - 1);
		}

		protected function scrollHandler(event : GScrollBarEvent) : void {
			if (event.direction == ScrollBarData.VERTICAL) {
				_textField.scrollV = event.position + 1;
			} else {
				_textField.scrollH = event.position;
			}
		}

		protected function textFieldScrollHandler(event : Event) : void {
			reset();
		}

		public function TextArea(data : TextAreaData) {
			_data = data;
			super(data);
		}

		public function showEdge(color : uint = 0x000000) : void {
			_textField.filters = [new GlowFilter(color, 1, 2, 2, 17, 1, false, false)];
		}

		public function hideEdge() : void {
			_textField.filters = null;
		}

		public function appendHtmlText(value : String, type : String = null) : String {
			_contentArr.push({str:value, type:type});
			var obj : Object;
			if (_contentArr.length > _data.maxRow ) {
				obj = _contentArr.shift();
				_textField.htmlText = "";
				for each (var content:Object in _contentArr)
					_textField.htmlText += content.str;
			} else
				_textField.htmlText += value;
			// _nowRow++;
			// if (_nowRow > _data.maxRow) clear();
			if (_data.maxLines > 0) {
				var lines : Array = _textField.htmlText.split(_data.edlim);
				if (lines.length - 1 > _data.maxLines) {
					_textField.htmlText = _textField.htmlText.slice(String(lines[0]).length + _data.edlim.length);
				}
			}
			if (!_lock) {
				_textField.scrollV = vScrollMax + 1;
			}
			if (obj)
				return obj.type;
			else
				return null;
		}

		public function appendRichText(newText : String, newSprites : Array = null, autoWordWrap : Boolean = false, format : TextFormat = null) : Boolean {
			_temptextField.htmlText = newText;
			_contentArr.push({text:newText, textlen:(_temptextField.length + ( newSprites?newSprites.length:0))});
			if (_contentArr.length > _data.maxRow ) {
				_output.replace(0, _contentArr[0].textlen +1, "");
				_contentArr.shift();
			}
			_output.append(newText, newSprites, autoWordWrap, format);
			return false;
		}

		public function appendText(value : String, color : uint = 0xCCCCCC) : void {
			_nowRow++;
			if (_nowRow > _data.maxRow) clear();
			var begin : int = _textField.text.length;
			_textField.appendText(value);
			_data.textFormat.color = color;
			_textField.setTextFormat(_data.textFormat, begin, _textField.text.length);
			_textField.scrollV = vScrollMax + 1;
		}

		public function set htmlText(value : String) : void {
			_textField.htmlText = value;
			if (!_lock) {
				_textField.scrollV = vScrollMax + 1;
			}
		}

		public function get htmlText() : String {
			return _textField.htmlText;
		}

		public function set text(value : String) : void {
			_textField.text = value;
			if (!_lock) {
				_textField.scrollV = vScrollMax + 1;
			}
		}

		public function get text() : String {
			return _textField.text;
		}

		public function get textField() : TextField {
			return _textField;
		}

		public function get richtextField() : RichTextField {
			return _output;
		}

		public function clear() : void {
			_output.clear();
			_contentArr.length = 0;
			_textField.text = "";
			_nowRow = 0;
		}

		public function upScroll() : void {
			_lock = true;
			if (_textField.scrollV > 0) {
				_textField.scrollV--;
			}
		}

		public function downScroll() : void {
			_lock = true;
			if (_textField.scrollV < vScrollMax + 1) {
				_textField.scrollV++;
			}
		}

		public function scrollToBottom(offset : int = 1) : void {
			_lock = false;
			_textField.scrollV = vScrollMax + offset;
		}

		public function get contentArr() : Array {
			return _contentArr;
		}

		public function set contentArr(contentArr : Array) : void {
			_contentArr = contentArr;
		}

		public function get temptextField() : TextField {
			return _temptextField;
		}

		public function set temptextField(temptextField : TextField) : void {
			_temptextField = temptextField;
		}
	}
}
